/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package intershopxssscanner.gui;

import intershopxssscanner.filters.FilterManager;
import intershopxssscanner.gui.files.FileTraversal;
import intershopxssscanner.parsers.FileParser;
import intershopxssscanner.parsers.HtmlHighlighterParser;
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

/**
 *
 * @author Willem
 */
public class GuiController {

    private final JFileChooser fc = new JFileChooser("c:");
    private GuiModel model;
    private FilterManager filterManager = new FilterManager();
    private static final String FILE_EXT = ".isml";
    private HtmlHighlighterParser htmlparser = new HtmlHighlighterParser();
    private int searchOffset = 0;

    GuiController(GuiModel model) {
        fc.setMultiSelectionEnabled(true);
        this.model = model;

        filterManager.loadFilterChain();
    }

    public void openFileBrowser(Component frame) {
        //open file browser       
        int returnVal = fc.showOpenDialog(frame);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File[] files = fc.getSelectedFiles();

            //add files to worklist model
            model.addFilesToWorkList(files);
        }
    }

    public void nextWorkListItem(JLabel workItemNameLabel) {
        FileParser fp = new FileParser();

        if (model.getModelWorkList().size() <= 0) {
            return;
        }

        File f = (File) model.getModelWorkList().getElementAt(0);
        if (f != null) {

            ArrayList<String> parseISMLFile = fp.parseISMLFile(f);
            ArrayList<String> oriISML = (ArrayList<String>) parseISMLFile.clone();

            parseISMLFile = filterManager.doChain(parseISMLFile);

            //displayXSSScan(parseISMLFile);    
            model.getWorkArea().setText(convertListToString(parseISMLFile));
            model.getWorkArea().setCaretPosition(0);

            workItemNameLabel.setText(f.toString());

            model.getFileViewTextPane().setText(convertListToString(oriISML));
            model.getFileViewTextPane().setCaretPosition(0);
        }

    }

    private String convertListToString(ArrayList<String> parseISMLFile) {
        StringBuilder sb = new StringBuilder();

        for (String l : parseISMLFile) {
            sb.append(l + "\n");
        }
        return sb.toString();
    }

    public void markWorkItemDone() {
        if (model.getModelWorkList().size() > 0) {
            Object remove = model.getModelWorkList().remove(0);
            model.getModelWorkDoneList().addElement(remove);

            model.getFileViewTextPane().setText("");
        }
    }

    public void traversfilesys(Component frame) {
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = fc.showOpenDialog(frame);


        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File dir = fc.getSelectedFile();

                new FileTraversal() {

                    @Override
                    public void onFile(final File f) {
                        if (f.getName().toLowerCase().matches(".*\\.isml$")) {
                            model.getModelWorkList().addElement(f);
                        }
                    }
                }.traverse(dir);
            } catch (IOException ex) {
                Logger.getLogger(GuiController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void searchFileView(String s) {
        /*String input = model.getFileViewTextPane().getText();
        String out = getHtmlparser().highLight(input, s);
        int loc = out.indexOf(s);
        model.getFileViewTextPane().setText(out);
        model.getFileViewTextPane().setCaretPosition(loc);*/

        Highlighter h = model.getFileViewTextPane().getHighlighter();
        h.removeAllHighlights();
        String text = model.getFileViewTextPane().getText();

        if (searchOffset < 0) {
            searchOffset = 0;
        }

        searchOffset = text.indexOf(s, searchOffset + 1);

        if (searchOffset < 0) {
            searchOffset = text.indexOf(s, 0);
        }


        if (searchOffset > 0) {
            try {
                h.addHighlight(searchOffset, searchOffset + s.length(), DefaultHighlighter.DefaultPainter);
                model.getFileViewTextPane().scrollRectToVisible(model.getFileViewTextPane().modelToView(searchOffset));
            } catch (BadLocationException ex) {
                Logger.getLogger(GuiController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * @return the htmlparser
     */
    public HtmlHighlighterParser getHtmlparser() {
        return htmlparser;
    }

    /**
     * @param htmlparser the htmlparser to set
     */
    public void setHtmlparser(HtmlHighlighterParser htmlparser) {
        this.htmlparser = htmlparser;
    }
}
